#include"DataStruct.h"
#include "math.h"

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <windows.h>

#define PI 3.1415926

int aim(DataStruct *data,BallStruct &ball,int &leftward,int &rightward)
{
    double x=ball.x,y=ball.y,v_x=ball.v_x,v_y=ball.v_y,cosA,angle_ball,angle;
    cosA=(y-data->plane1.y)/sqrt((x-data->plane1.x)*(x-data->plane1.x)+(y-data->plane1.y)*(y-data->plane1.y));
    angle_ball=acos(cosA)*180/PI;
    angle=fmod(fmod(data->plane1.angle-angle_ball,360)+360,360);
    if(angle<3.6||angle>356.4)
        return 1;
    else if(angle<180)
    {
        leftward=1;
        rightward=0;
    }
    else
    {
        leftward=0;
        rightward=1;
    }
    return 0;
}


void ai_pve(DataStruct*data,KeyStruct*key){
    //默认不移动且恒发射子弹
//    typedef struct KeyStruct{
//        int id;
//        int forward;
//        int rotate_left;
//        int rotate_right;
//        int shoot;
//        int shield;
//    }KEYSTRUCT;
    key->forward = false;
    key->rotate_left = false;
    key->rotate_right = false;
    key->shoot = true;
    key->shield = true;

    //创建静态移动标志
    static int moveFlag = 1, des_x, des_y;

    //标志为0则正在移动或检查发现需要移动
//    if (moveNeeded(data, des_x, des_y) || moveFlag == 0)
//    {
////        key->shield = true;
//        //进行移动并返回移动结果
//        moveFlag = moveTo(data, des_x, des_y, key->forward, key->rotate_left, key->rotate_right);
//        //返回标志为-1则发生错误
//        if (moveFlag == -1)
//        {
//            std::cout << "移动发生错误" << std::endl;
//        }
//        //结束函数执行
//        return;
//    }

    //如果球个数不为零
    if (data->ball_size)
    {
        //瞄准零号球
        aim(data, data->ball[0], key->rotate_left, key->rotate_right);
    }
}
